﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Drawing;
using System.IO;
using System.Text;
using System.IO.Compression;

namespace BattleThis.Helpers
{
	/// <summary>
	/// Result of dyed a template svg file.
	/// </summary>
	public class SvgColorTemplateResult
	{

		#region Variablen
		/// <summary>
		/// MimeTyp of the Template-Result.
		/// </summary>
		public const string MimeType = "image/svg+xml";
		/// <summary>
		/// Fileextension if the result was saved with <see cref="SaveAsGzip"/>.
		/// </summary>
		public const string ZippedFileExtension = ".svgz";
		/// <summary>
		/// Fileextension if the result was saved with <see cref="Save"/>.
		/// </summary>
		public const string UnZippedFileExtension = ".svg";

		/// <summary>
		/// The Search-Pattern for svgz files.
		/// </summary>
		public const string SearchPatternsSvgz = "*.svgz";

		#endregion

		#region Properties

		/// <summary>
		/// Gets or sets the name of the file.
		/// </summary>
		/// <value>
		/// The name of the file.
		/// </value>
		public string FileName { get; set; }

		/// <summary>
		/// Gets or sets the color which was used.
		/// </summary>
		/// <value>
		/// The dyed in.
		/// </value>
		public Color DyedIn { get; protected set; }

		/// <summary>
		/// Gets or sets the dyed value, this is the raw svg text value.
		/// </summary>
		/// <value>
		/// The dyed value.
		/// </value>
		protected string DyedValue { get;  set; }
		
		#endregion

		#region Methoden
		/// <summary>
		/// Initializes a new instance of the <see cref="SvgColorTemplateResult"/> class.
		/// </summary>
		/// <param name="value">The value.</param>
		/// <param name="newColor">The new color.</param>
		public SvgColorTemplateResult( string value, Color newColor )
		{
			if( string.IsNullOrEmpty( value ) ) throw new ArgumentNullException( "value" );

			this.DyedValue = value;
			this.DyedIn = newColor;
			this.FileName = System.IO.Path.GetFileNameWithoutExtension( System.IO.Path.GetTempFileName() );
		}

		/// <summary>
		/// Saves the result to the specified path as a new svg file.
		/// </summary>
		/// <param name="pathToSave">The path to save.</param>
		/// <returns></returns>
		public FileSystemInfo Save( string pathToSave )
		{

			var fullPath = this.CreateFullPath( pathToSave,UnZippedFileExtension );

			System.IO.File.WriteAllText( fullPath, this.DyedValue );
			return new FileInfo( fullPath );
		}

		/// <summary>
		/// Saves the result to the specified path as a new svgz file.
		/// </summary>
		/// <param name="pathToSave">The path to save.</param>
		/// <returns></returns>
		public FileSystemInfo SaveAsGzip( string pathToSave )
		{
			var fullPath = this.CreateFullPath( pathToSave, ZippedFileExtension );

			var zippedResult = this.Compress( fullPath );
			return zippedResult;

		}

		/// <summary>
		/// Compresses the <see cref="DyedValue"/> and saved it to the specified path.
		/// </summary>
		/// <param name="pathToSave">The path to save.</param>
		/// <returns>FileInfo-Object to the saved file.</returns>
		public FileSystemInfo Compress( string pathToSave )
		{
			UTF8Encoding encoding = new UTF8Encoding( false );

			byte[] buffer = encoding.GetBytes( this.DyedValue );
			using( MemoryStream memoryStream = new MemoryStream() )
			{
				using( GZipStream gZipStream = new GZipStream( memoryStream, CompressionMode.Compress, true ) )
				{
					gZipStream.Write( buffer, 0, buffer.Length );

				}
				memoryStream.Position = 0;

				byte[] compressedData = new byte[ memoryStream.Length ];
				memoryStream.Read( compressedData, 0, compressedData.Length );

				System.IO.FileStream oFileStream = null;
				oFileStream = new System.IO.FileStream( pathToSave, System.IO.FileMode.Create );
				oFileStream.Write( compressedData, 0, compressedData.Length );
				oFileStream.Close();

				return new FileInfo( pathToSave );

			}
		}

		/// <summary>
		/// Creates the full path incl. path and filename.
		/// </summary>
		/// <param name="pathToSave">The path to save.</param>
		/// <returns></returns>
		private string CreateFullPath( string pathToSave, string fileExtension )
		{
			if( string.IsNullOrEmpty( pathToSave ) ) throw new ArgumentNullException( "pathToSave" );

			var cleanFileName = Path.GetFileNameWithoutExtension( this.FileName );
			var fullFileName = string.Concat( cleanFileName, fileExtension );

			return System.IO.Path.Combine( pathToSave, fullFileName );
		}

		#endregion		

	}
}